#include "ModelMesh.h"

ModelMesh::ModelMesh(char* name, BYTE numVertexBuffers, vector<EnginePointer<ModelVertexBuffer>>* vertexBuffers,
		      UINT* vertexBufferIndexes, EnginePointer<ModelIndexBuffer>& indexBuffer,
			  D3DXVECTOR3 boundingBoxCenter, D3DXVECTOR3 boundingBoxExtents,
			  UINT numSubsets, vector<EnginePointer<MeshSubset>>* subsets, UINT* subsetIndexes)
{
	UNREFERENCED_PARAMETER(boundingBoxExtents);
	UNREFERENCED_PARAMETER(boundingBoxCenter);

	strcpy_s(this->name, MAX_MESH_NAME, name);
	this->numVertexBuffers = numVertexBuffers;
	for (UINT i = 0; i < MAX_VERTEX_STREAMS; i++)
	{
		d3dVertexBuffers[i] = NULL;
	}
	for (UINT i = 0; i < numVertexBuffers; i++)
	{
		this->vertexBuffers[i] = vertexBuffers->at(vertexBufferIndexes[i]);
		strides[i] = (UINT)this->vertexBuffers[i]->getStride();
	}
	this->indexBuffer = indexBuffer;
	for (UINT i = 0; i < numSubsets; i++)
	{
		this->subsets.push_back(subsets->at(subsetIndexes[i]));
	}
}

HRESULT ModelMesh::LoadGraphicsContent(EnginePointer<ID3D10Device> pDevice)
{
	for (UINT i = 0; i < numVertexBuffers; i++)
	{
		d3dVertexBuffers[i] = vertexBuffers[i]->getBuffer();
		d3dVertexBuffers[i]->AddRef();
	}

	return S_OK;
}

void ModelMesh::UnloadGraphicsContent()
{
	unsigned long refs;
	for (UINT i = 0; i < MAX_VERTEX_STREAMS; i++)
	{
		if (d3dVertexBuffers[i] != NULL) refs = d3dVertexBuffers[i]->Release();
		d3dVertexBuffers[i] = NULL;
	}
}

ID3D10Buffer** ModelMesh::getVertexBuffers()
{
	return d3dVertexBuffers;
}

BYTE ModelMesh::getNumVertexBuffers()
{
	return numVertexBuffers;
}

UINT* ModelMesh::getStrides()
{
	return strides;
}

EnginePointer<ID3D10Buffer> ModelMesh::getIndexBuffer()
{
	return indexBuffer->getBuffer();
}

list<EnginePointer<MeshSubset>>::iterator ModelMesh::subsetBegin()
{
	return subsets.begin();
}

list<EnginePointer<MeshSubset>>::iterator ModelMesh::subsetEnd()
{
	return subsets.end();
}